var util = require('../../utils/util.js');
var api = require('../../utils/api.js');

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    type:'',
    number:'',
    question:'',
    question_types:'',
    pic: '',
    items: [
      { name: 'A', value: '' },
      { name: 'B', value: '' },
      { name: 'C', value: '' },
      { name: 'D', value: '' }
    ],
    answer: '',
    chapter: '',
    explain: '',
    result:0,
    display:'',
    test_4:'',
    submit:false,
    statistics:{
      total:100,
      error:0,
      right:0,
      unanswer:0,
      score:0,
    },
    disabled:false
  },
  onLoad: function () {
     var type   = wx.getStorageSync('type');
     this.data.type = type;
     var test_4 = wx.getStorageSync(type+'_test_4');
     if(!test_4){
       wx.showLoading({
         mask:true,
         title: '智能出题中',
       })
        this.getTestQuestion(type);
     }else{
       var number = wx.getStorageSync(type + '_number_test_4');
       number = number ? number : 0;
       this.getQuestion(number);
     }
    
  },
  getTestQuestion:function(type){
    api.get({
      url: 'Question/getTestList',
      data: {
        type: type,
        subject:4
      },
      success: res => {
        if (res) {
           wx.setStorageSync(type + '_test_4', res);
           this.getQuestion(0);
           wx.hideLoading();
        }
      },
    });
  },

  radioChange: function (e) {
    var type = this.data.type;
    var test_4 = wx.getStorageSync(type + '_test_4');
    var number = this.data.number;
    var v = e.detail.value;
    var a = this.data.answer;

    var result = 1;
    if (v.length != a.length) {
      result = 0;
    } else {
      for (var i = 0; i < v.length; i++) {
        if (a.indexOf(v[i]) == -1) {
          result = 0;
          break;
        }
      }
    }
    test_4[number - 1]['result'] = result;
    test_4[number - 1]['is_answer'] = v;
    wx.setStorageSync(type + '_test_4', test_4);
  },

  radioChange1:function(e){
    var type = this.data.type;
    var test_4 = wx.getStorageSync(type + '_test_4');
    var number = this.data.number;
    var v = e.detail.value;
    var result = (v == this.data.answer)? 1 : 0;
    test_4[number-1]['result']    = result;
    test_4[number - 1]['is_answer'] = v;
    wx.setStorageSync(type + '_test_4', test_4);
  },

  goTest: function (event){
    var number = event.currentTarget.id;
    this.getQuestion(number);
    this.setData({
      submit: false,
    })
  },

  upQuestion:function(){
     this.setData({result:0,display:0})
     if(this.data.number<2) return;
     this.getQuestion(this.data.number-2);
  },
  downQuestion:function(){
    if (this.data.number > 49){
      wx.showToast({
        title: '已经是最后一题了',
        icon: 'none',
        mask:true,
        duration: 1000
      })
    }else{
      this.setData({ result: 0, display: 0 })
      this.getQuestion(this.data.number);
    }
  },
  goIndex:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  getQuestion:function(number){
     var type = wx.getStorageSync('type');
     var question_info = wx.getStorageSync(type + '_test_4')[number];
     var question_types = question_info.question_types;
     var disabled       = question_info.is_answer == 0 ? false : true;
     if(disabled){
       this.setData({ display:1})
     }
     this.setData({
       number: Number(number)+1,
       question: question_info.question,
       question_types: question_info.question_types,
       answer: question_info.answer,
       chapter: question_info.chapter,
       explain: question_info.explain,
       pic: question_info.pic,
       is_answer: question_info.is_answer,
       disabled: disabled,
       result: question_info.result
      })
     if (question_types == '1' || (question_types == '3' && question_info.is_answer==0)) {
       this.setData({
         items: [
           { name: 'A', value: question_info.option1 },
           { name: 'B', value: question_info.option2 },
           { name: 'C', value: question_info.option3 },
           { name: 'D', value: question_info.option4 }
         ]
       })
     }
     
     if (question_types == '3' && question_info.is_answer != 0) {

        var answer_str = question_info.is_answer.join('');
        var items = [{ name: 'A', value: question_info.option1 },
                     { name: 'B', value: question_info.option2 },
                     { name: 'C', value: question_info.option3 },
                     { name: 'D', value: question_info.option4 }];
        if(answer_str.indexOf("A") != -1 ){
          items[0]['checked'] = 'true';
        }
        if (answer_str.indexOf("B") != -1) {
          items[1]['checked'] = 'true';
        }
        if (answer_str.indexOf("C") != -1) {
          items[2]['checked'] = 'true';
        }
        if (answer_str.indexOf("D") != -1) {
          items[3]['checked'] = 'true';
        }
        
        this.setData({
          items: items
        })
        
     }

     if (question_types == '2') {
       this.setData({
         items: [
           { name: '对', value: '正确' },
           { name: '错', value: '错误' },
         ]
       })
     }
      wx.setStorageSync(type+'_number_test_4', number);
  },
  submit:function(){
     var test_4 = wx.getStorageSync(this.data.type + '_test_4');
     //统计
     this.statisticsResult();
     this.setData({
       test_4: test_4,
       submit:true,
     })
  },
  doTest:function(){
    this.setData({submit:false})
  },
  statisticsResult:function(){
    var test_4 = wx.getStorageSync(this.data.type + '_test_4');
    var total  = test_4.length;

    var error    = 0;
    var right    = 0;
    var unanswer = 0;
    for (var j = 0, len = total; j < len; j++) {
        if(test_4[j].is_answer == 0){
          unanswer += 1;
        }else{
          if (test_4[j].result == 0){
            error+=1;
          }
          if (test_4[j].result == 1) {
            right += 1;
          }
        }
    }
    
    this.setData({
      statistics: {
        total: total,
        error: error,
        right: right,
        unanswer: unanswer,
        score: right*2,
      }
    })

  },
  endTest:function(){
    var test_result= {
      total: this.data.statistics.total,
      error: this.data.statistics.error,
      right: this.data.statistics.right,
      unanswer: this.data.statistics.unanswer,
      score: this.data.statistics.score,
      type : this.data.type,
      subject:'四',
    }
    wx.setStorageSync('test_result', test_result);
    wx.redirectTo({
      url: '/pages/result/result',
    })
  }
})
